import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class ClientLoadIn implements ActionListener {
    private JLabel label1, label2, label3;
    private JTextField tf1, tf2;
    private JButton button;
    private JPanel panel;
    private JFrame jf;

    public static void main(String[] args) {
        new ClientLoadIn("127.0.0.1", "65525");
    }

    public ClientLoadIn(String host, String port) {
        label1 = new JLabel("服务端地址：");
        label2 = new JLabel("服务端端口：");
        tf1 = new JTextField();
        tf1.setText(host);
        tf2 = new JTextField();
        tf2.setText(port);
        panel = new JPanel();
        button = new JButton("确认");
        jf = new JFrame("德州扑克 - 连接");
        label3 = new JLabel();
        button.addActionListener(this);
        /* 布局设置 */
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateContainerGaps(true);
        layout.setAutoCreateGaps(true);
        // 水平组
        GroupLayout.SequentialGroup hSequentialGroup = layout.createSequentialGroup().addComponent(label1)
                .addComponent(tf1);
        GroupLayout.SequentialGroup hSequentialGroup2 = layout.createSequentialGroup().addComponent(label2)
                .addComponent(tf2);
        GroupLayout.SequentialGroup hSequentialGroup3 = layout.createSequentialGroup().addComponent(button).addGap(30)
                .addComponent(label3);
        GroupLayout.ParallelGroup hGroup = layout.createParallelGroup().addGroup(hSequentialGroup)
                .addGroup(hSequentialGroup2).addGroup(hSequentialGroup3);
        layout.setHorizontalGroup(hGroup);
        // 垂直组
        GroupLayout.ParallelGroup vParallelGroup = layout.createParallelGroup().addComponent(label1).addComponent(tf1);
        GroupLayout.ParallelGroup vParallelGroup2 = layout.createParallelGroup().addComponent(label2).addComponent(tf2);
        GroupLayout.ParallelGroup vParallelGroup3 = layout.createParallelGroup().addComponent(button).addGap(30)
                .addComponent(label3);
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup().addGroup(vParallelGroup)
                .addGroup(vParallelGroup2).addGroup(vParallelGroup3);
        layout.setVerticalGroup(vGroup);
        jf.add(panel);
        jf.setVisible(true);
        jf.setSize(285, 135);
        jf.setLocation(400, 200);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setResizable(false);
        jf.setLocationRelativeTo(null);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            if (tf1.getText().isEmpty()) {
                label3.setText("服务端地址不可为空");
            } else if (tf2.getText().isEmpty()) {
                label3.setText("服务端端口不可为空");
            } else {
                label3.setText(null);
                Test.setServerHost(tf1.getText());
                try {
                    Test.setServerProt(Integer.valueOf(tf2.getText()));
                    if (Test.getServerPort() < 0 || 65535 < Test.getServerPort())
                        throw new NumberFormatException();
                } catch (NumberFormatException nfe) {
                    label3.setText("服务端端口输入非法(0~65535)");
                    return;
                }
                Test.createLink();
                try {
                    Test.link.connect();
                } catch (IOException ioe) {
                    label3.setText("暂时无法连接到该服务器");
                    System.err.println("暂时无法连接至服务器[" + Test.getServerHost() + ':' + Test.getServerPort() + "].");
                    return;
                }
                jf.setVisible(false);
                new LoadIn();
            }
        }
    }
}
